package com.iwhalecloud.bss.kite.cucc.service.custview;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.CustViewBaseVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.CustViewVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.Fee;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.FeeInfoVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.HistoryFeeInfoVO;

import com.ztesoft.bss.common.util.DateUtil;

/**
 * 成员资费信息
 */
public class GetMemberFeeInfoTask extends CustViewBaseTask {
    public GetMemberFeeInfoTask(Object[] args) {
        super(args);
    }

    @Override
    public CustViewBaseVO call() throws Exception {
        Object[] args = getArgs();
        if (args.length != 2) {
            return null;
        }
        Map<String, Object> grpUserDetailMapData = (Map<String, Object>) args[0];
        CustViewVO custViewVO = (CustViewVO) args[1];
        FeeInfoVO feeInfoVO = new FeeInfoVO();
        HistoryFeeInfoVO hisFeeInfoVO = new HistoryFeeInfoVO();
        custViewVO.setFee(feeInfoVO);
        custViewVO.setHistoryFee(hisFeeInfoVO);
        List<Fee> resultfees = Lists.newArrayList();
        List<Fee> resultHisfees = Lists.newArrayList();
        if (KiteMapUtils.isNotEmpty(grpUserDetailMapData) && !KiteObjectUtils
            .isEmpty(grpUserDetailMapData.get("goods"))) {
            List<Map<String, Object>> goodsList = (List<Map<String, Object>>) grpUserDetailMapData.get("goods");
            goodsList.stream().forEach(goods -> {
                List<Map<String, Object>> feesList = (List<Map<String, Object>>) goods.get("fees");
                if (KiteListUtils.isNotEmpty(feesList)) {
                    for (Map<String, Object> feesMap : feesList) {
                        Fee fee = new Fee();
                        fee.setFeeCode(KiteMapUtils.getString(feesMap, "code"));
                        // 资费名称
                        fee.setFeeName(KiteMapUtils.getString(feesMap, "feeName"));
                        // 资费描述
                        fee.setFeeDesc(KiteMapUtils.getString(feesMap, "feeDesc"));
                        String startDate = "";
                        Date dStartDate = DateUtil.strToDate(KiteMapUtils.getString(feesMap, "startDate"));
                        startDate = DateUtil.format(dStartDate);
                        fee.setStartDate(startDate);
                        Date dEndDate = DateUtil.strToDate(KiteMapUtils.getString(feesMap, "endDate"));
                        String endDate = "";
                        endDate = DateUtil.format(dEndDate);
                        fee.setEndDate(endDate);
                        Date nowDate = new Date();
                        if (dEndDate.after(nowDate)) { //资费信息
                            resultfees.add(fee);
                        }
                        else { // 历史资费信息
                            resultHisfees.add(fee);
                        }
                    }
                }
            });
            if (KiteListUtils.isNotEmpty(resultfees)) {
                feeInfoVO.setFees(resultfees);
            }
            if (KiteListUtils.isNotEmpty(resultHisfees)) {
                hisFeeInfoVO.setFees(resultHisfees);

            }
        }
        return feeInfoVO;
    }
}
